package weibo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import weibo.common.result.JsonResult;
import weibo.mapper.CommentMapper;
import weibo.pojo.dto.CommentDTO;
import weibo.pojo.entity.Comment;
import weibo.pojo.entity.User;
import weibo.pojo.vo.CommentList;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "03.评论模块")
@RestController
@RequestMapping("/v1/comment")
@Slf4j
@Validated
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    // 发表评论  判断用户的登录状态 状态码 1 成功 2 未登录
    @PostMapping("/insert")
    @ApiOperation(value = "发表评论")
    public JsonResult insert(@RequestBody @Validated CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.info("评论信息：{}",commentDTO);
        // 判断用户是否登录
        User user=(User) session.getAttribute("user");
        log.debug("用户信息：{}",user);
        /*if (user==null){
            return 2;
        }*/
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(user.getId());
        comment.setCreated(new Date());
        log.debug("评论信息：{}",comment);
        commentMapper.insert(comment);
        log.info("评论成功，评论信息：{}",comment);
        return JsonResult.success();
    }

    // 获取评论列表
    @GetMapping("/selectByWeiboId")
    @ApiOperation(value = "获取评论列表")
    @ApiImplicitParam(name = "id",value = "微博id",required = true)
    public JsonResult selectByWeiboId(@RequestParam @Range(min = 1,message = "微博id不正确") int id){
        log.info("微博id：{}",id);
        return JsonResult.success(commentMapper.selectByWeiboId(id));
    }

}
